package Sevlet;



import bean.person;
import util.DbUtil;
import util.TouristDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.io.IOException;
import java.sql.*;

public class registerServlet extends HttpServlet {
    public void wrong1(){
        String msg="不允许有空，注册失败！";
        int type= JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    public void wrong2(){
        String msg="两次密码不同，注册失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    public void wrong3(){
        String msg="用户名已存在，注册失败！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    public void wrong4(){
        String msg="抱歉！账号不能以staff开头！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    public void wrong5(){
        String msg="联系电话输入有误！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    public void right(){
        String msg="注册信息合格，注册成功！";
        int type=JOptionPane.YES_NO_CANCEL_OPTION;
        String title="信息提示";
        JOptionPane.showMessageDialog(null, msg, title, type);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName=new String(req.getParameter("userName").getBytes("ISO-8859-1"),"UTF-8");
        String id = new String(req.getParameter("id").getBytes("ISO-8859-1"),"UTF-8");
        String password1=new String(req.getParameter("password1").getBytes("ISO-8859-1"),"UTF-8");
        String password2=new String(req.getParameter("password2").getBytes("ISO-8859-1"),"UTF-8");
        String name=new String(req.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
        String sex=new String(req.getParameter("sex").getBytes("ISO-8859-1"),"UTF-8");
        String birth=req.getParameter("year")+"-"+req.getParameter("month")+"-"+req.getParameter("day");
        String nation = "汉族";
        String phone=new String(req.getParameter("phone").getBytes("ISO-8859-1"),"UTF-8");
        System.out.println(phone);
        String place=new String(req.getParameter("place").getBytes("ISO-8859-1"),"UTF-8");
        System.out.println(userName+'-'+id+'-'+password1+'-'+name+'-'+sex+'-'+birth+'-'+nation+'-'+phone+'-'+place);

        int flag = DbUtil.isNum(phone);
        if (phone.length()!=11 || flag!=2){
            wrong5();
            resp.sendRedirect("tourist/register.jsp");
        }else if(userName.length()==0||password1.length()==0||password2.length()==0||name.length()==0||phone.length()==0){
            wrong1();
            resp.sendRedirect("tourist/register.jsp");
        }else if(!(password1.equals(password2))){
            wrong2();
            resp.sendRedirect("tourist/register.jsp");
        }else if (userName.indexOf("staff")==0){
            wrong4();
            resp.sendRedirect("tourist/register.jsp");
        }else if (userName.equals("root")){
            wrong3();
            resp.sendRedirect("tourist/register.jsp");
        }
        else {          /*查数据库。*/
            person tourist = null;
            TouristDao tdao = new TouristDao();
            try {
                tourist = tdao.get(userName);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if (tourist == null){     //用户名未被注册
                tourist = new person();
                tourist.setUserName(userName);
                tourist.setPassword(password1);
                tourist.setName(name);
                tourist.setId(id);
                tourist.setSex(sex);
                tourist.setBirth(birth);
                tourist.setNation(nation);
                tourist.setPhone(phone);
                tourist.setPlace(place);
                try {
                    tdao.addTourist(tourist);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                right();

                HttpSession session = req.getSession();
                session.setAttribute("person", tourist);

                resp.sendRedirect("http://localhost:8080/index.jsp");
            }else {          //用户名已被注册
                wrong3();
                resp.sendRedirect("tourist/register.jsp");
            }
        }
    }
}
